<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-commitment" lay-title="短信管理"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="topic-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">承诺书内容</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="context" autocomplete="off"
                                                   class="layui-input"
                                                   placeholder="请输入内容">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="reset">
                                    重置
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="insert">
                                    新增
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="commitmentTable" lay-data="{id: 'commitmentTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="option">
    <a lay-event="detail" class="website-green">查看</a>
    <a lay-event="edit" class="website-blue">修改</a>
    <a lay-event="del" class="website-red">删除</a>
</script>
<!--表格中状态的按钮-->
<script type="text/html" id="commitment-enable">
    <input type="checkbox" {{ d.isEnabled
           ? 'checked' : '' }} value="{{d.commitmentId}}" lay-filter="enabled_status" lay-skin="switch" lay-text="启用|取消"/>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website', 'dict', 'laydate'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            $view = $('#website-commitment'),
            $insert = $view.find('#insert'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns
        ;
        form.render();
        initTable();

        form.on('switch(enabled_status)', function (obj) {
            website.post(ctx + 'biz/commitment/enable', {
                commitmentId: this.value,
                isEnabled: obj.elem.checked
            }, function (d) {
                if (d.code == 200) {
                    website.alert.success('修改成功');
                    $query.click();
                } else {
                    obj.elem.checked = !obj.elem.checked;
                    form.render();
                }
            });
        });
        $insert.on('click', function () {
            website.modal.open('新增内容', 'biz/commitment/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#website-commitment-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#website-commitment-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });


        /**
         * 表格的排序点击
         **/
        table.on('sort(commitmentTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });


        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'commitmentTable',
                url: ctx + 'biz/commitment/list',
                cols: [[
                    {type: 'numbers', title: '序号',},
                    {field: 'context', title: '承诺书内容', width: '70%'},
                    {title: '是否启用', templet: '#commitment-enable', width: '10%'},
                    {title: '操作', toolbar: '#option', width: '10%'}
                ]
                ]
            });
        }

        table.on('tool(commitmentTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                // 查看详情
                website.modal.view('查看承诺书', 'biz/commitment/detail/' + data.commitmentId, {
                    area: $(window).width() <= 750 ? '95%' : '50%',
                });
            }
            if (layEvent === 'del') {
                website.modal.confirm('删除信息', '确定删除该信息？', function () {
                    website.post(ctx + 'biz/commitment/delete/' + data.commitmentId, null, function () {
                        website.alert.success('删除成功');
                        $query.click();
                    });
                });
            }
            if (layEvent === 'edit') {
                // 修改
                website.modal.open('修改信息', 'biz/commitment/update/' + data.commitmentId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#website-commitment-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        })

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            return $searchForm.serializeJson();
        }

    })
</script>